package org.csu.gp_store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.gp_store.entity.Orders;
import org.csu.gp_store.mapper.OrderMapper;
import org.csu.gp_store.service.OrderService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public int insertOrder(Orders orders) {
        return  orderMapper.insert(orders);
    }

    @Override
    public int deleteOrder(@NotNull Long orderID) {
        return orderMapper.deleteById(orderID);
    }

    @Override
    public int updateOrder(@NotNull Long orderID, @NotNull Orders orders) {
        UpdateWrapper<Orders>updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("orderId",orderID);
        return orderMapper.update(orders,updateWrapper);
    }

    @Override
    public int updateOrderStatus(@NotNull Long orderID, int status) {
         UpdateWrapper<Orders> updateWrapper=new UpdateWrapper<>();
         updateWrapper.eq("orderID",orderID).set("status",status);
         return orderMapper.update(null,updateWrapper);
    }


    @Override
    public List<Orders> getOrderListByAccountID(@NotNull Long accountID) {
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("accountID",accountID);
        return orderMapper.selectList(queryWrapper);
    }

    @Override
    public List<Orders> getOrderListByAccountIDAndStatus(@NotNull Long accountID, int status) {
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("accountID",accountID).eq("status",status);
        return orderMapper.selectList(queryWrapper);
    }

    @Override
    public Orders getOrderByOrderID(@NotNull Long orderID) {
        return orderMapper.selectById(orderID);
    }
}
